package com.beordie.filter;

import com.beordie.model.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname AuthorityFilter
 * @Description TODO
 * @Date 2021/7/12 21:01
 * @Created 30500
 */
public class AuthorityFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String url = request.getRequestURI();
        System.out.println(url);
        if ("/login.html".equals(url) || "/admin/login.udo".equals(url)) {
            System.out.println("首页放行|登陆放行");
            filterChain.doFilter(request, response);
        } else if (request.getSession().getAttribute("username") != null) {
            System.out.println("权限放行");
            filterChain.doFilter(request, response);
        } else {
            response.sendRedirect("/login.html");
        }
    }

    @Override
    public void destroy() {

    }
}
